﻿using Direct.IDAL.Manager.Money;
using DirectSell.Common.Url;
using DirectSell.Model.Money;
using DirectSell.Model.Query;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.DAL.Manager.UserCenter
{
    public class VirtualMoneyAuditRepository:BaseManagerRepository, IVirtualMoneyAuditRepository
    {

        #region 查询电子币申请记录
        /// <summary>
        /// 查询电子币申请记录
        /// </summary>
        /// <returns></returns>
        public async Task<PagedList<VirtualMoney>> GetRecord(BaseCondition condition)
        {
            PageParam param = new PageParam();
            param.PageIndex = condition.pageIndex == 0 ? 1 : condition.pageIndex;
            param.PageSize = condition.pageSize;
            param.OrderBy = "State ASC";
            param.TableName = "VirtualMoneyApply";
            
            StringBuilder sb = new StringBuilder();
            sb.Append(" 1=1");
            if (condition.SerachCondition != null)
            {
                sb.AppendFormat("AND (UserName LIKE '%{0}%' OR UserCode  LIKE '%{0}%' ) ", condition.SerachCondition.Trim());
            }
            if (!string.IsNullOrEmpty(condition.startTime))
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND ApplyTime>='{0}' and ApplyTime<='{1}' ", condition.startTime, condition.endTime);
                }
                else
                {
                    sb.AppendFormat(" AND ApplyTime>='{0}' and ApplyTime<='{1}' ", condition.startTime, DateTime.Now.ToString());
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND ApplyTime>='2010-01-01' and ApplyTime<='{1}' ", condition.endTime);
                }
            }
            // sb.Append(" ORDER BY State ASC ");
            param.StrWhere = sb.ToString();
            return await base.PageQuery<VirtualMoney>(param);
        }
        #endregion

        #region 修改审核状态
        /// <summary>
        /// 修改审核状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="hstable"></param>
        public void UpdateState(int id,int state,ref Hashtable hstable)
        {
            string sql = string.Format("UPDATE VirtualMoneyApply SET State={0},HandleTime=GETDATE() WHERE Id={1} AND State=0", state,id);
            hstable.Add(sql, null);
        }
        #endregion

        #region 增加电子币
        /// <summary>
        /// 增加电子币
        /// </summary>
        /// <param name="usercode"></param>
        /// <param name="id"></param>
        /// <param name="hstable"></param>
        public void AddMoney(string usercode,int id,ref Hashtable hstable)
        {
            string sql = string.Format("SELECT Amount FROM VirtualMoneyApply WHERE Id={0}", id);
            decimal Amount = base.QueryFirstT<decimal>(sql);
            StringBuilder sb = new StringBuilder();
            sb.Append(" UPDATE UserInforMations SET  ");
            sb.AppendFormat(" VirtualMoney+={0}  WHERE UserCode='{1}' ", Amount,usercode);
            hstable.Add(sb.ToString(), null);
        }
        #endregion

        #region 减少电子币
        /// <summary>
        /// 减少电子币
        /// </summary>
        /// <param name="usercode"></param>
        /// <param name="id"></param>
        /// <param name="hstable"></param>
        public void ReduceMoney(string usercode, int id, ref Hashtable hstable)
        {
            string sql = string.Format("SELECT Amount FROM VirtualMoneyApply WHERE Id={0}", id);
            decimal Amount = base.QueryFirstT<decimal>(sql);
            StringBuilder sb = new StringBuilder();
            sb.Append(" UPDATE UserInforMations SET  ");
            sb.AppendFormat(" VirtualMoney-={0}  WHERE UserCode='{1}' ", Amount, usercode);
            hstable.Add(sb.ToString(), null);
        }
        #endregion

        #region 查询金额
        /// <summary>
        /// 查询金额
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<decimal> QueryAmount(int id)
        {
            string sql = string.Format("SELECT Amount FROM VirtualMoneyApply WHERE Id={0}", id);
            return await base.QueryFirstTAsync<decimal>(sql);
        }
        #endregion

        #region 类型
        /// <summary>
        /// 查询金额
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> QueryType(int id)
        {
            string sql = string.Format("SELECT Type FROM VirtualMoneyApply WHERE Id={0}", id);
            return await base.QueryFirstTAsync<int>(sql);
        }
        #endregion


    }
}
